package com.ruoyi.ihome.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ihome.domain.Search;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ihome.domain.District;
import com.ruoyi.ihome.service.IDistrictService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域Controller
 *
 * @author mage
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/ihome/district")
public class DistrictController extends BaseController {
    @Autowired
    private IDistrictService districtService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:list')")
    @GetMapping("/list")
    public TableDataInfo list(District district) {
        startPage();
        List<District> list = districtService.selectDistrictList(district);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('ihome:district:list')")
    @GetMapping("/district")
    public List<Search> selectDistrict() {
        return districtService.selectDistrict();
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, District district) {
        List<District> list = districtService.selectDistrictList(district);
        ExcelUtil<District> util = new ExcelUtil<District>(District.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(districtService.selectDistrictById(id));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody District district) {
        return toAjax(districtService.insertDistrict(district));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody District district) {
        return toAjax(districtService.updateDistrict(district));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('ihome:district:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(districtService.deleteDistrictByIds(ids));
    }
}
